import { loginByUsername, loginuserphone,phoneBackLogin,sendLoginCode,logout, getUserInfo, getMenus } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    user: '',
    status: '',
    code: '',
    token: getToken(),
    name: '',
    tenantNo: '',
    bigScreenUrl: '',
    id: '',
    fsiteNo: '',
    fsiteName: '',
    fsiteLevel: '',
    avatar: '',
    introduction: '',
    roles: [],
    perms: [],
    setting: {
      articlePlatform: []
    },
    menus: []// 菜单
  },

  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_TENANTNO: (state, tenantNo) => {
      state.tenantNo = tenantNo
    },
    SET_BIGSCREENURL: (state, bigScreenUrl) => {
      state.bigScreenUrl = bigScreenUrl
    },
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_FSITENO: (state, fsiteNo) => {
      state.fsiteNo = fsiteNo
    },
    SET_FSITENAME: (state, fsiteName) => {
      state.fsiteName = fsiteName
    },
    SET_FSITELEVEL: (state, fsiteLevel) => {
      state.fsiteLevel = fsiteLevel
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMS: (state, perms) => {
      state.perms = perms
    },
    SET_MENUS: (state, menus) => {
      state.menus = menus
    }
  },

  actions: {
    // 用户名登录
    LoginByUsername({ commit }, userInfo) {
      const id = userInfo.id
      // const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        loginByUsername(id, userInfo.password).then(response => {
          const data = response.data.data
          const tokenStr = data.tokenHead + data.token
          commit('SET_TOKEN', tokenStr)
          setToken(tokenStr)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    //手机号账号登录
    loginuserphone({ commit }, userInfo) {
      // const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        loginuserphone(userInfo).then(response => {
          console.log('login',response)
          const data = response.data.data
          const tokenStr = data.tokenHead + data.token
          commit('SET_TOKEN', tokenStr)
          setToken(tokenStr)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    //手机验证码登录
    phoneBackLogin({ commit }, userInfo) {
      // const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        phoneBackLogin(userInfo).then(response => {
          const data = response.data.data
          const tokenStr = data.tokenHead + data.token
          commit('SET_TOKEN', tokenStr)
          setToken(tokenStr)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    //获取验证码
    sendLoginCode({ commit }, userInfo) {
      //const id = userInfo.id
      // const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        sendLoginCode(userInfo).then(response => {
          //const data = response.data.data
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取用户信息
    GetUserInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getUserInfo(state.token).then(response => {
          console.log('getUserInfo',response)
          const data = response.data.data

          if (data.perms && data.perms.length > 0) { // 验证返回的perms是否是一个非空数组
            commit('SET_PERMS', data.perms)
          } else {
            reject('该用户没有任何权限 !') // getInfo: perms must be a non-null array
          }

          commit('SET_ROLES', data.roles)
          commit('SET_NAME', data.name)
          commit('SET_TENANTNO', data.tenantNo)
          commit('SET_BIGSCREENURL', data.bigScreenUrl)
          commit('SET_ID', data.id)
          commit('SET_FSITENO', data.fsiteNo)
          commit('SET_FSITENAME', data.fsiteName)
          commit('SET_FSITELEVEL', data.fsiteLevel)
          commit('SET_AVATAR', data.avatar)
          commit('SET_INTRODUCTION', data.introduction)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取菜单
    getMenus({ commit, state }) {
      return new Promise((resolve, reject) => {
        getMenus(state.token).then(response => {
          console.log('getMenus',response )
          const data = response.data.data
          if (data.list && data.list.length > 0) { // 验证返回的perms是否是一个非空数组
            commit('SET_MENUS', data.list)
          }
          resolve(data)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 第三方验证登录
    // LoginByThirdparty({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMS', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },

    // 动态修改权限
    ChangeRoles({ commit, dispatch }, role) {
      return new Promise(resolve => {
        commit('SET_TOKEN', role)
        setToken(role)
        getUserInfo(role).then(response => {
          const data = response.data
          commit('SET_ROLES', data.roles)
          commit('SET_PERMS', data.perms)
          commit('SET_NAME', data.name)
          commit('SET_BIGSCREENURL', data.bigScreenUrl)
          commit('SET_ID', data.id)
          commit('SET_FSITENO', data.fsiteNo)
          commit('SET_FSITENAME', data.fsiteName)
          commit('SET_FSITELEVEL', data.fsiteLevel)
          commit('SET_AVATAR', data.avatar)
          commit('SET_INTRODUCTION', data.introduction)
          dispatch('GenerateRoutes', data) // 动态修改权限后 重绘侧边菜单
          resolve()
        })
      })
    }
  }
}

export default user
